// Author: wsfuyibing <682805@qq.com>
// Date: 2025-07-02

package services

import (
	"context"
	"gitee.com/go-libs/db-xorm/db"
	"server/app/errs"
	"server/app/http/models"
	"server/app/kind"
)

// LatestVisible
// 可用优惠券.
func (o *CouponService) LatestVisible(ctx context.Context, aid kind.ArcadeId, uid kind.UserId, typ kind.OrderKind) (list []*models.Coupon, err error) {
	var sess *db.Session
	if sess, err = o.Dao.Slaver(ctx); err != nil {
		err = errs.ErrDatabaseQuery
		return
	}
	if err = sess.Where("arcade_id = ? AND state = ? AND user_id = ? AND type = ?",
		aid,
		kind.CouponStateVisible,
		uid,
		typ,
	).OrderBy(`available_end ASC`).Limit(10).Find(&list); err != nil {
		err = errs.ErrDatabaseQuery
	}
	return
}
